import { createRouter, createWebHistory } from 'vue-router'


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect:'/home'
    },
    {
      path: '/home',
      name: 'home',
      component: () => import('../views/HomeView.vue')
    },
    {
      path: '/cart',
      name: 'cart',
      component: () => import('../views/CartView.vue')
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/LoginView.vue'),
      meta:{
        flag:true
      }
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('../views/RegisterView.vue'),
      meta:{
        flag:true
      },
      children:[
        {
          path:"/register/step1",
          name:"step1",
          component: () => import('../components/Step1Com.vue'),
        }, 
        {
          path:"/register/step2",
          name:"step2",
          component: () => import('../components/Step2Com.vue'),
        }, 
        {
          path:"/register/step3",
          name:"step3",
          component: () => import('../components/Step3Com.vue'),
        }, 
      ]
    },
    { 
      path: '/user', 
      name: 'user', 
      component: () => import('../views/UserView.vue'),
      meta:{
        flag:true
      }, 
    },
    { 
      path: '/address', 
      name: 'address', 
      component: () => import('../components/UserAddress.vue'),
      meta:{
        flag:true
      }, 
    },
    { 
      path: '/order', 
      name: 'order', 
      component: () => import('../components/UserOrder.vue'),
      meta:{
        flag:true
      }, 
    },
    { 
      path: '/details/:proid', 
      name: 'details', 
      component: () => import('../views/DetailsView.vue'),
      meta:{
        flag:true
      }, 
    },
    { 
      path: '/:pathMatch(.*)*', 
      name: 'NotFound', 
      component: () => import('../views/NotfindView.vue'),
      meta:{
        flag:true
      },  
    },
  ]
})


router.beforeEach((to)=>{
  if(to.path==="/cart" || to.path==="/user"){
    if(localStorage.getItem("token")){
      return true
    }else{
      return {"path":"/login"}
    }
  }else if(to.path==="/login"){
    if(localStorage.getItem("token")){
      return {"path":"/user"}
    }else{
      return true
    }
  }else{
    return true
  }
  
})







export default router
